package org.example.org.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.example.base.annotation.ExamplePermission;
import org.example.base.util.AjaxResult;
import org.example.base.util.PageInfo;
import org.example.org.domain.Department;
import org.example.org.query.DepartmentQuery;
import org.example.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;
@Api(value = "部门管理",description = "部门操作的CRUD")
@RequestMapping("/department")
@RestController//该controller接口中均返回数据
@ExamplePermission(name = "部门管理",desc="管理部门的CRUD")
public class DepartmentController {
    @Autowired
    private IDepartmentService service;

    //增/改 put
    @ApiOperation(value="更新或新增")
    @PutMapping
    @ExamplePermission(name = "部门保存",desc="管理部门的新增或修改")
    public AjaxResult save(@RequestBody Department department){
        try {
            if(Objects.isNull(department.getId())){
                service.insert(department);
            }else{
                service.update(department);
            }
            return AjaxResult.success();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }

    }
    //删delete
    @DeleteMapping("/{id}")
    @ExamplePermission(name = "部门删除",desc="部门的删除")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            service.delete(id);
            return AjaxResult.success();

        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    @GetMapping({"/{id}"})
    @ExamplePermission(name = "查询指定部门",desc="根据ID查询部门")
    public AjaxResult selectByID(@PathVariable("id") Long id){
        try {
            Department department = service.selectById(id);
            return AjaxResult.success(department);

        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    //查全部 get
    @GetMapping
    @ExamplePermission(name = "查询所有部门",desc="查询所有部门不分页")
    public AjaxResult selectAll(){
        try {
            List<Department> departments = service.selectAll();
            return AjaxResult.success(departments);

        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    //查一个 get
    @GetMapping("/deptTree")
    @ExamplePermission(name="部门树查询",desc = "查询部门树为父部门提供数据")
    public AjaxResult getDeptTree(){

        try {
            List<Department> department = service.getDeptTree();
            return AjaxResult.success(department);
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    @PostMapping
    @ExamplePermission(name="分页查询",desc = "部门的分页查询")
    public AjaxResult queryPage(@RequestBody DepartmentQuery departmentQuery){
        try {
            PageInfo pageInfo=service.queryPage(departmentQuery);
            return AjaxResult.success(pageInfo);

        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    @PatchMapping
    @ExamplePermission(name="批量删除",desc = "部门的批量删除")
    public AjaxResult batchDelete(@RequestBody List<Long> ids){
        try {
            service.batchDelete(ids);
            return AjaxResult.success();

        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }
}
